$(function(){
	var setting = {
		check: {
			enable: true
		},
		data: {
			simpleData: {
				enable: true
			}
		},
		view: {
			showIcon: false
		}
	};
	var roleId = $("#roleId").val();
	$.ajax({
		url : baselocation+"/system/menu/menuList",
		type : "POST",
		data : {"id":roleId},
		success : function(result) {
			if(result.code == 0) {
				
				$.fn.zTree.init($("#menuTree"), setting, result.data);
	   			$.fn.zTree.getZTreeObj("menuTree").expandAll(true);
	   			
			}else if(result.code == 1) {
				layer.alert(result.message);
			}
		}
	})
	$("#roleSubmit").click(function(){
		if(!$("#roleSubmit").valid())
			return false;
		var nodes = $.fn.zTree.getZTreeObj("menuTree").getCheckedNodes(true);
		var menuIds = '';
		for(var i = 0; i < nodes.length; i++){
			menuIds += nodes[i].id;
			if(i != nodes.length - 1)
				menuIds += ',';
		}
		$('#menuIds').val(menuIds);
		$.ajax({
			type: 'post',
			url: baselocation+'/system/role/add',
			data: $('#roleform').serialize(),
			success: function(result){
				if(result.code == 0) {
					var successMsg = "";
					if(roleId == "") {
						successMsg = "新增角色成功";
					}else{
						successMsg = "修改角色成功";
					}
					
					layer.msg(successMsg, {
						icon : 1,
						time : 1000
					}, function() {
						window.parent.location.reload(); // 刷新父页面
					});
				}else {
					layer.msg(result.message, {
						icon : 2,
						time : 1000
					});
				}
			}
		});
		return false;
	})
})